$(function() {
   $("[name=study_date]").calendar({
        onSelect: function(date){
            var calendarDate = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();

                    $("#curriculumTable_datagrid").datagrid({
                        url:'/curriculumTable/selectCurriculumTableByDate?date='+calendarDate
                    })
            }
        });

    //begin列表信息
    $("#curriculumTable_datagrid").datagrid({
        url: "/curriculumTable/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100,hidden:true},
            {field: "date", title: "日期", width: 100, },
            {field: "week", title: "星期", width: 100,formatter:function(value,row,index){
              /*  alert(row.date)*/
              if(row.date){
                var date = new Date(row.date).getDay();
               if(date == 0){
                   return "星期日"
               }else if (date == 1){
                   return "星期一"
               }else if (date == 2){
                   return "星期二"
               }else if (date == 3){
                   return "星期三"
               }else if (date == 4){
                   return "星期四"
               }else if (date == 5){
                    return "星期五"
                }else if (date == 6){
                    return "星期六"
                }
             }}},
            {field: "grade", title: "班级", width: 100,formatter:function(value,row,index){
                if(value){
                    return value.name;
                }
            }},
            {field: "curriculum", title: "课程名称", width: 100,formatter:function(value,row,index){
                if(row.curriculum){
                    return row.curriculum.name;
                }
            }},
            {field: "headTeacher", title: "班主任", width: 100,formatter:function(value,row,index){
                if(row.headTeacher){
                    return row.headTeacher.username;
                }
            }},
            {field: "subjecrTeacher", title: "上课老师", width: 100,formatter:function(value,row,index){
                if(row.subjectTeacher){
                    return row.subjectTeacher.username;
                }
            }},
            {field: "classRoom", title: "教室", width: 100,formatter:function (value,row,index) {
                if(row.classRoom){
                   return row.classRoom.name;
                }
            }},
            {field: "remark", title: "备注", width: 100},
            {field: "state", title: "状态", width: 100,  formatter:function(value,row,index){
                if(value == 0){
                    return "<font color='red'>未开班</font>";
                }else if(value == 1){
                    return "<font color='green'>已开班</font>";
                }}

               },



        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:3,
        pageList:[3,5,10,20],
        toolbar: "#curriculumTable_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#curriculumTable_dialog").dialog({
        width: 500,
        height: 500,
        buttons: "#curriculumTable_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#curriculumTable_form").form("clear");
            $("#curriculumTable_dialog").dialog("setTitle", "课程表添加");
            $("#curriculumTable_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {
            $("#curriculumTable_form").form("clear");
            $("#curriculumTable_dialog").dialog("setTitle", "课程表编辑");
            var row = $("#curriculumTable_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            //班级回显
            if(row.grade){
                row["grade.id"]=row.grade.id
            }
            //课程回显
            if(row.curriculum){
                row["curriculum.id"]=row.curriculum.id
            }
            //班主任
            if(row.headTeacher){
                row["headTeacher.id"]=row.headTeacher.id
            }
            //上课老师
            if(row.subjectTeacher){
                row["subjectTeacher.id"]=row.subjectTeacher.id
            }
            //教室
            if(row.classRoom){
                row["classRoom.id"]=row.classRoom.id
            }
            $("#curriculumTable_form").form("load", row);
            $("#curriculumTable_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#curriculumTable_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var curriculumTableId = $("#curriculumTableId").val();
            var url;
            if (curriculumTableId) {
                url = "/curriculumTable/update"
            } else {
                url = "/curriculumTable/save"
            }
            $("#curriculumTable_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#curriculumTable_dialog").dialog("close");
                        $("#curriculumTable_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove:function(){
            var row = $("#curriculumTable_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认','您确认要操作此行吗？',function(r){
                if (r){
                    //注意这里的id要从当前选中的行中获取
                    var curriculumTableId = row.id;
                    $.get("/curriculumTable/delete",{curriculumTableId:curriculumTableId},function(data){
                        if(!data.success){
                            $.messager.alert('温馨提示', data.msg);
                        }else{
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#curriculumTable_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload:function (){
            $("#curriculumTable_datagrid").datagrid("reload")
        },
        //end刷新按钮

        //begin高级查询按钮
        queryKeyWord:function (){
            var gradeName=$("#gradeName").combobox("getValue");
            var headTeacher=$("#headTeacher").combobox("getValue");
            var classRoom=$("#classRoom").combobox("getValue");

            $("#curriculumTable_datagrid").datagrid("load",{
                gradeName:gradeName,
                headTeacher:headTeacher,
                classRoom:classRoom,
            })
        },


        select:function (){
            var beginDate=$("#beginDate").combobox("getValue");
            var endDate=$("#endDate").combobox("getValue");
            $("#curriculumTable_datagrid").datagrid("load",{
                beginDate:beginDate,
                endDate:endDate,

            })
        }
        //end高级查询按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});

    //begin学科的查询功能
function selectClassName(){
   var id =  $("#curriculumSeries").combobox("getValue");
   console.log(id)
    $("#easyui-datagrid").datagrid({
        url:'/curriculum/selectCurriculumName?curriculumSeriseId='+id,
        columns: [[

            {field: "name", title: "课程名称", width: 280}
        ]]
    })

    }
//end学科的查询功能